package com.power.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.cache.RedisCacheConfiguration;
import org.springframework.data.redis.serializer.RedisSerializationContext;
import org.springframework.data.redis.serializer.RedisSerializer;

import java.time.Duration;

/**
 * redis缓存配置类
 */
@Configuration
public class RedisCacheConfig {
@Bean
    public RedisCacheConfiguration redisCacheConfiguration(){
    RedisCacheConfiguration redisCacheConfiguration = RedisCacheConfiguration.defaultCacheConfig();
    redisCacheConfiguration.serializeValuesWith(RedisSerializationContext.SerializationPair.fromSerializer(RedisSerializer.json()))//设置序列化方式为json
            .entryTtl( Duration.ofDays(7))//设置缓存过期时间默认为七天
            .disableCachingNullValues();//redis缓的value禁止使用null值



    return redisCacheConfiguration;
}
}
